<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\Product\Product;
use Exception;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

//商品分类
class ProductController extends Controller
{

    public function list(Request $request)
    {
        try {
            $param = $request->all();
            $Product = new Product();
            $data = $Product->getAllProduct($param);
            return success($data);
        } catch (Exception $e) {
            return error(null, $e->getMessage());
        }
    }
    public function add(Request $request)
    {
        try {
            $data = $request->all();

            $validator = Validator::make(
                $data,
                [
                    'name' => 'required|unique:zlg_product,name',
                    'price' => 'required',
                    'stock_quantity' => 'required',
                    'image' => 'required',
                    'category_id' => 'required',
                ],
                [
                    'name.required' => '名称不能为空',
                    'name.unique' => '名称必须唯一',
                    'price.required' => '库存不能为空',
                    'stock_quantity.required' => '名称不能为空',
                    'image.required' => '商品图片不能为空',
                    'category_id.required' => '分类不能为空',
                ]
            );

            if ($validator->fails()) {
                throw new Exception($validator->errors()->first());
            }
            // dump($data);die;
            $Product = new Product();
            $Product->name = $data['name'];
            $Product->price = $data['price'];
            $Product->stock_quantity = $data['stock_quantity'];
            $Product->image = $data['image'];
            $Product->category_id = $data['category_id'];
            $res = $Product->save();
            if (!$res) {
                throw new Exception('添加失败');
            }
            return success();
        } catch (Exception $e) {
            return error(null, $e->getMessage());
        }
    }

    public function show(Request $request)
    {
        try {
            $data = $request->all();
            $validator = Validator::make(
                $data,
                [
                    'id' => 'required',
                ],
                [
                    'id.required' => 'id不能为空',
                ]
            );
            if ($validator->fails()) {
                throw new Exception($validator->errors()->first());
            }
            $Category = new Product();
            $res = $Category->where("id", $data['id'])->first();
            if (!$res) {
                throw new Exception('添加失败');
            }
            return success($res);
        } catch (Exception $e) {
            return error(null, $e->getMessage());
        }
    }


    public function save(Request $request)
    {
        try {
            $data = $request->all();
            $id = $data['id'] ?? null;
            $validator = Validator::make(
                $data,
                [
                    'id' => 'required',
                    'name' => 'required|unique:zlg_product,name,' . $id, // 修改这里
                    'price' => 'required',
                    'stock_quantity' => 'required',
                    'image' => 'required',
                    'category_id' => 'required',
                ],
                [
                    'id.required' => 'id不能为空',
                    'name.required' => '名称不能为空',
                    'name.unique' => '名称必须唯一',
                    'price.required' => '库存不能为空',
                    'stock_quantity.required' => '名称不能为空',
                    'image.required' => '商品图片不能为空',
                    'category_id.required' => '分类不能为空',
                ]
            );
            if ($validator->fails()) {
                throw new Exception($validator->errors()->first());
            }
            $Category = new Product();

            $res = $Category->where("id", $data['id'])->update($data);
            if (!$res) {
                throw new Exception('添加失败');
            }
            return success();
        } catch (Exception $e) {
            return error(null, $e->getMessage());
        }
    }

    public function del(Request $request)
    {
        try {
            $data = $request->all();
            $validator = Validator::make(
                $data,
                [
                    'id' => 'required',
                ],
                [
                    'id.required' => 'id不能为空',
                ]
            );
            if ($validator->fails()) {
                throw new Exception($validator->errors()->first());
            }
            $Category = new Product();
            $res = $Category->where("id", $data['id'])->delete();
            if (!$res) {
                throw new Exception('删除失败');
            }
            return success();
        } catch (Exception $e) {
            return error(null, $e->getMessage());
        }
    }
}
